/**
 * 
 */
package LDVH.save;


import LDVH.itf.ISave;
import LDVH.itf.ILivre;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author 3403024
 */
public class Saver implements ISave {
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 */
	private final ILivre livre;
        
        public Saver(ILivre livre) {
            this.livre = livre;
        }
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param chemin
	 * @return
	 */
        @Override
	public boolean saveLivre(String chemin) {
            if ( ! chemin.matches("^.*\\.ldvh$"))
                chemin += ".ldvh";
            
            ObjectOutputStream out = null;		
            FileOutputStream fileOut = null;
            boolean result;
            
            try {
                fileOut = new FileOutputStream(chemin);
                out = new ObjectOutputStream(fileOut);
                out.writeObject(livre);
                result = true;
            }
            catch (IOException ex) {
                Logger.getLogger(Saver.class.getName()).log(Level.SEVERE, null, ex);
                result = false;
            } 
            finally {
                try {
                    out.close();
                    fileOut.close();
                }
                catch (IOException ex) {
                    Logger.getLogger(Saver.class.getName()).log(Level.SEVERE, null, ex);
                    result = false;
                }
            }
            return result;
	}
	
}